# -*- coding:utf-8 -*-

"""
外键，数据库+id：user_id
"""

from django.db import models
from django.db.models import Q

from Apps.common.custommodel import CustomModel, ModifyModel
from Apps.User.models import User

import logging

logger = logging.getLogger('django')

"""
{
  "path": "/proTable/useProTable/detail/:id",
  "name": "useProTableDetail",
  "component": "/proTable/useProTable/detail",
  "meta": {
    "icon": "Menu",
    "title": "树形 ProTable",
    "isLink": "",
    "isHide": false,
    "isFull": false,
    "isAffix": false,
    "isKeepAlive": true
  }
}
/**
 * @description 📚 路由参数配置简介
 * @param path ==> 路由菜单访问路径
 * @param name ==> 路由 name (对应页面组件 name, 可用作 KeepAlive 缓存标识 && 按钮权限筛选)
 * @param redirect ==> 路由重定向地址
 * @param component ==> 视图文件路径
 * @param meta ==> 路由菜单元信息
 * @param meta.icon ==> 菜单和面包屑对应的图标
 * @param meta.title ==> 路由标题 (用作 document.title || 菜单的名称)
 * @param meta.activeMenu ==> 当前路由为详情页时，需要高亮的菜单
 * @param meta.isLink ==> 路由外链时填写的访问地址
 * @param meta.isHide ==> 是否在菜单中隐藏 (通常列表详情页需要隐藏)
 * @param meta.isFull ==> 菜单是否全屏 (示例：数据大屏页面)
 * @param meta.isAffix ==> 菜单是否固定在标签页中 (首页通常是固定项)
 * @param meta.isKeepAlive ==> 当前路由是否缓存
 * */
"""


class Menu(CustomModel):
    TYPE_CHOICES = (
        (0, "菜单"),
        (1, "按钮"),
    )
    parentId = models.CharField(max_length=50, verbose_name="上级菜单", default="", null=True)
    type = models.SmallIntegerField(choices=TYPE_CHOICES, verbose_name="类型")

    path = models.CharField(max_length=128, verbose_name="路由菜单访问路径", default="", null=True,
                            help_text="路由菜单访问路径")
    name = models.CharField(max_length=128, verbose_name="路由 name", default="", null=True,
                            help_text="路由 name (对应页面组件 name, 可用作 KeepAlive 缓存标识 && 按钮权限筛选)")
    component = models.CharField(max_length=256, verbose_name="视图文件路径", default="", null=True)
    meta = models.JSONField(default=dict, verbose_name="路由菜单元信息", null=True)

    orderNum = models.IntegerField(verbose_name="排序", default=0)
    status = models.BooleanField(default=True, verbose_name="菜单状态,是否启用")

    creator = models.ForeignKey(to=User, db_constraint=False, related_name="system_menu_creator",
                                on_delete=models.SET_NULL, verbose_name="创建人", null=True)
    modifier = models.ForeignKey(to=User, db_constraint=False, related_name="system_menu_modifier",
                                 on_delete=models.SET_NULL, verbose_name="修改人", null=True)

    class Meta:
        db_table = 'system_menu'
        verbose_name = "菜单表"
        ordering = ('orderNum', 'create_time')
        unique_together = ('parentId', 'name')


class Role(CustomModel):
    roleName = models.CharField(max_length=64, verbose_name="角色名称")
    status = models.BooleanField(default=True, verbose_name="角色状态")
    defaultRole = models.BooleanField(default=False, verbose_name="是否默认角色")
    remark = models.TextField(verbose_name="备注", help_text="备注", null=True, blank=True)
    user = models.ManyToManyField(to=User, db_constraint=False, verbose_name="用户")
    menu = models.ManyToManyField(to=Menu, db_constraint=False, verbose_name="菜单")
    creator = models.ForeignKey(to=User, db_constraint=False, related_name="system_role_creator",
                                on_delete=models.SET_NULL, verbose_name="创建人", null=True)
    modifier = models.ForeignKey(to=User, db_constraint=False, related_name="system_role_modifier",
                                 on_delete=models.SET_NULL, verbose_name="修改人", null=True)

    class Meta:
        db_table = 'system_role'
        verbose_name = "角色表"
        ordering = ('-create_time',)
